/* Copyright 2013 David Wolverton
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.sbbs.genericdao.dao.demo;

import java.io.Serializable;
import javax.annotation.PostConstruct;


import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.internal.SessionImpl;
import org.sbbs.genericdao.dao.jpa.GenericDAOImpl;
import org.sbbs.genericdao.search.MetadataUtil;
import org.sbbs.genericdao.search.jpa.JPAAnnotationMetadataUtil;
import org.sbbs.genericdao.search.jpa.JPASearchProcessor;
import org.sbbs.genericdao.search.jpa.hibernate.HibernateJpaMetadataUtil;

/**
 * Extension of GenericDAOImpl that is configured for Autowiring with Spring or
 * J2EE.
 */

public class BaseDAOImpl<T, ID extends Serializable> extends GenericDAOImpl<T, ID> {

     @PersistenceContext(unitName = "DEFAULT")
    private EntityManager em;

    @PostConstruct
    public void init() {
        super.setEntityManager(em);
//        MetadataUtil metadataUtil = null;
//        EntityManagerFactoryImpl emf = (EntityManagerFactoryImpl) em.getEntityManagerFactory();
//        metadataUtil = HibernateJpaMetadataUtil.getInstanceForEntityManagerFactory(((SessionImpl) this.em.unwrap(SessionImpl.class)).getSessionFactory());
          MetadataUtil hmt = new JPAAnnotationMetadataUtil();
        JPASearchProcessor searchProcessor = new JPASearchProcessor(hmt);
        super.setSearchProcessor(searchProcessor);
    }
    
//	@PersistenceContext
//	@Override
//	public void setEntityManager(EntityManager entityManager) {
//		super.setEntityManager(entityManager);
//	}
//
//	@Autowired
//	@Override
//	public void setSearchProcessor(JPASearchProcessor searchProcessor) {
//		super.setSearchProcessor(searchProcessor);
//	}

}
